package com.lhl.security.utils;

import lombok.Data;

import java.util.HashMap;

/**
 * 统一返回结果的类
 * @author Admin
 */
@Data
public class R {

    private final static Integer SUCCESS = 200;
    private final static Integer ERROR = 500;

    private Boolean success;

    private Integer code;

    private String message;

    private Object data;


    private R() {
    }

    /**
     * 成功静态方法
     *
     * @return R
     */
    public static R ok() {
        R r = new R();
        r.setSuccess(true);
        r.setCode(SUCCESS);
        r.setMessage("成功");
        return r;
    }

    /**
     * 失败静态方法
     *
     * @return R
     */
    public static R no() {
        R r = new R();
        r.setSuccess(false);
        r.setCode(ERROR);
        r.setMessage("失败");
        return r;
    }

    public R success(Boolean success) {
        this.setSuccess(success);
        return this;
    }

    public R message(String message) {
        this.setMessage(message);
        return this;
    }

    public R code(Integer code) {
        this.setCode(code);
        return this;
    }

    public R data(Object data) {
        this.data = data;
        return this;
    }

    public R data(String key, Object value) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put(key,value);
        this.data = hashMap;
        return this;
    }
}
